package com.example.wisdomclassroomapi.facade.controller.api;

import com.example.wisdomclassroomapi.common.cache.RedisCache;
import com.example.wisdomclassroomapi.common.util.IdentificationUtil;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.LiveDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

@RestController
@RequestMapping("/api/live")
@Slf4j
public class LiveController extends BaseController {
    @Resource
    private RedisCache cache;

    @PostMapping("/start")
    public LiveDto startLive(@RequestBody @Valid LiveDto dto) {
        Map<String, LiveDto> map = new HashMap<>();
        Map<String, List<String>> findMap = cache.getCacheMap("liveFind");

        String id = IdentificationUtil.shortUuid();

        dto.setTeacherId(this.getLoginUserId());
        dto.setId(id);
        map.put(id, dto);

        List<String> gradeList = findMap.get(dto.getGradeId());
        List<String> subjectList = findMap.get(dto.getSubjectId());

        if (Objects.isNull(gradeList)) {
            gradeList = new ArrayList<>();
        }
        gradeList.add(id);
        findMap.put(dto.getGradeId(), gradeList);

        if (Objects.isNull(subjectList)) {
            subjectList = new ArrayList<>();
        }
        subjectList.add(id);
        findMap.put(dto.getSubjectId(), subjectList);

        cache.setCacheMap("liveRoom", map);
        cache.setCacheMap("liveFind", findMap);

        return dto;
    }

    @PostMapping("/list")
    public Map<String, Object> listLiveRoom() {
        return cache.getCacheMap("liveRoom");
    }

    @PostMapping("/getLive")
    public List<LiveDto> getLive(@RequestBody @Valid LiveDto dto) {
        Map<String, LiveDto> liveRoom = cache.getCacheMap("liveRoom");
        List<LiveDto> result = new ArrayList<>();
        if (StringUtils.isNotBlank(dto.getId())) {
            LiveDto dto1 = liveRoom.get(dto.getId());

            result.add(dto1);
            return result;
        }

        Map<String, List<String>> findMap = cache.getCacheMap("liveFind");
        if (StringUtils.isNotBlank(dto.getGradeId())) {
            List<String> liveRooms = new ArrayList<>();
            if (Objects.nonNull(findMap.get(dto.getGradeId()))) liveRooms.addAll(findMap.get(dto.getGradeId()));
            liveRooms.forEach(s -> {
                LiveDto liveDto = liveRoom.get(s);
                result.add(liveDto);
            });
            return result;
        }
        if (StringUtils.isNotBlank(dto.getSubjectId())) {
            List<String> liveRooms = new ArrayList<>();
            if (Objects.nonNull(findMap.get(dto.getSubjectId()))) liveRooms.addAll(findMap.get(dto.getSubjectId()));
            liveRooms.forEach(s -> {
                LiveDto liveDto = liveRoom.get(s);
                result.add(liveDto);
            });
            return result;
        }

        return result;
    }

    @GetMapping("/end")
    public void endLive(String id) {
        Map<String, LiveDto> liveRoom = cache.getCacheMap("liveRoom");
        liveRoom.remove(id);
    }
}
